package com.example.textview.PLC;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class Fx_Commend extends ViewModel {


    private IOnValueChangedListener valueChangedListener;

    public void setValueChangedListener(IOnValueChangedListener valueChangedListener) {
        this.valueChangedListener = valueChangedListener;
    }

    public Fx_Commend(String adr, String dataType) {
        this.adr = adr;
        this.dataType = dataType;

        result = new MutableLiveData<>();

        if (dataType.equals("bool")) {
            Boolean b = new Boolean(false);
            result.postValue(b);

        } else if (dataType.equals("word")) {
            Integer i = new Integer(0);
            result.postValue(i);

        } else {
            Float f = new Float(0.0);
            result.postValue(f);
        }
    }


    public Fx_Commend(String adr, String dataType, int weight) {

        this.adr = adr;
        this.dataType = dataType;
        this.weight = weight;
        this.weight_current = weight;
        result = new MutableLiveData<>();

        if (dataType.equals("bool")) {
            Boolean b = new Boolean(false);
            result.postValue(b);

        } else if (dataType.equals("word")) {
            Integer i = new Integer(0);
            result.postValue(i);

        } else {
            Float f = new Float(0.0);
            result.postValue(f);
        }
        //  result.setValue(0);
    }

    private String adr = "";

    private int weight = 0;

    public void setWeight(int weight) {
        this.weight = weight;
    }

    private int weight_current = 0;


    private String dataType = "bool";

    private Object valueSet;

    public Object getValueSet() {
        return valueSet;
    }

    public void setValueSet(Object valueSet) {
        this.valueSet = valueSet;
    }


    private MutableLiveData<Object> result;


    public MutableLiveData<Object> getResult() {
        return result;
    }

    public void setResult(Object result) {


        if (result != null)
            try {




                if (!this.result.getValue().equals(result)) {
                    this.result.postValue(result);
                    if (this.valueChangedListener != null) {


                        this.valueChangedListener.onValueChanged(getAdr(), result, this.getResult().getValue());

                    }
                    // Log.i("plc1",getAdr());

                }

            } catch (Exception e) {

            }


    }

    public void setAdr(String adr) {
        this.adr = adr;
    }


    public boolean isPermit() {
        boolean b = weight_current >= weight;

        if (b)
            weight_current = 0;
        else weight_current++;

        return b;

    }


    public String getAdr() {


        return adr;

    }


    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public String getDataType() {
        return dataType;
    }


    public void setWeight_current() {
        this.weight_current = weight;
    }

    public interface IOnValueChangedListener {
        void onValueChanged(String adr, Object newValue, Object previouslyValue);
    }
}
